	package com.cn.yunhe.rentcars.web;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.DeptMapper;
import com.cn.yunhe.rentcars.model.Dept;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 门店页面控制器
 * 
 * @author 朱钦明
 *
 */

@RestController
public class StoreController {
	/**
	 * 从容器加载DeptMapper对象
	 */
	@Autowired
	private DeptMapper deptMapper;
	
	/**
	 * 页面跳转到storelist页面
	 * @return ModelAndView 跳转到storelist页面
	 */
	@RequestMapping(value="/storelist",method=RequestMethod.GET)
	public ModelAndView storeList(){
		return new ModelAndView("storelist");
	}
	
	/**
	 * 页面跳转到storeadd页面
	 * @return 返回ModelAndView 跳转到storeadd页面
	 */
	@RequestMapping(value="/storeadd",method=RequestMethod.GET)
	public ModelAndView storeAdd(){
		
		return new ModelAndView("storeadd");
	}
	
	/**
	 * 跳转到storeedit页面
	 * @return 返回ModelAndView对象 ,这个对象包含了要修改的信息
	 */
	@RequestMapping(value="/storeedit",method=RequestMethod.GET)
	public ModelAndView storeEdit(@RequestParam String id){
		Dept dept = deptMapper.getDeptById(id);
		Map<String,Dept> map = new HashMap<String,Dept>();
		map.put("dept", dept);
		return new ModelAndView("storeedit",map);
	}
	
	/**
	 * 跳转到storeget页面
	 * @return 返回ModelAndView对象跳转到storeget页面
	 */
	@RequestMapping(value="/storeget",method=RequestMethod.GET)
	public ModelAndView gradeGet(@RequestParam String id){
		Dept dept = deptMapper.getDeptById(id);
		
		/*
		 * 把时间格式化从字符串形式
		 */
		Date date =dept.getAddTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(date);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("dept", dept);
		map.put("dateStr", dateStr);
		return new ModelAndView("storeget",map);
	}
	
	/**
	 * 查询所有部门信息并返回给页面
	 * @return Map 包含返回给页面的信息
	 * 
	 */
	@RequestMapping(value="/getStores",method=RequestMethod.GET)
	public Map<String,Object> getStores(@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		int pageNum = (start/length)+1;
		int pageSize = length;
		
		PageHelper.startPage(pageNum, pageSize);
		List<Dept> deptList = deptMapper.getDepts();
		PageInfo<Dept> page = new PageInfo<Dept>(deptList);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("draw", draw);
		map.put("data", page.getList());
		map.put("recordsTotal", page.getTotal());
		map.put("recordsFiltered", page.getTotal());
		return map;
	}
	
	/**
	 * 插入门店信息到门店信息表
	 * 
	 */
	@RequestMapping(value="/addstores",method=RequestMethod.POST)
	public void addStores(@RequestBody Dept dept){
		dept.setDeptId(UUID.randomUUID().toString());
		dept.setAddTime(new Date());
		deptMapper.addDepts(dept);
	}
	
	/**
	 * 修改门店信息表
	 * @return void
	 * @param 前台页面传入的Dept对象数据
	 * @param 要修改的对象的deptid
	 */
	@RequestMapping(value="/editstore/{id}",method=RequestMethod.PUT)
	public void editStore(@PathVariable("id") String id,@RequestBody Dept dept){
		dept.setDeptId(id);
		dept.setAddTime(new Date());
		deptMapper.updateDepts(dept);
	}
	
	/**
	 * 删除门店信息
	 * @return void
	 * @param id获取前台页面传入的部门编号
	 */
	@RequestMapping(value="/deletestore",method=RequestMethod.POST)
	public void deleteStore(@RequestParam String id){
		deptMapper.deleteDept(id);
	}
	
	/**
	 * 批量删除门店信息
	 * @return void
	 * @param id前台页面传入的字符串
	 */
	@RequestMapping(value="/deletestores",method=RequestMethod.POST)
	public void deleteStores(@RequestParam String id){
		
		List<String> list = Arrays.asList(id.split(","));
		deptMapper.deleteDeprts(list);
	}
}
